package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Employee;
import com.itheima.pojo.R;
import com.itheima.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,
                             HttpServletRequest resq) {

        //前台获取账号密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //将用户名与数据库中查询
        //select * from biao where username = username
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Employee::getUsername, username);

        Employee one = employeeService.getOne(lambdaQueryWrapper);
        //MD5 加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        if (one == null) {
            //employeeService.save(one);
            //resq.getSession().setAttribute("employee",one.getId());
            return R.error("用户名错误");
        }

        if (!password.equals(one.getPassword())) {
            return R.error("密码错误");
        }

        if (one.getStatus() == 0) {
            return R.error("该用户已经被禁用");
        }

        resq.getSession().setAttribute("one", one.getId());
        return R.success(one);

    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest resq) {
        HttpSession session = resq.getSession();
        session.removeAttribute("one");
        return R.success("退出成功");

    }

    @GetMapping("/page")
    public R<IPage> add(Integer page, Integer pageSize, String name) {
        //条件查询
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //StringUtils 是commons下的需要依赖
        //功能进行非空判断
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Employee::getUsername, name);

        //分页查询
        IPage<Employee> pa = new Page<>(page, pageSize);
        employeeService.page(pa, lambdaQueryWrapper);
        return R.success(pa);

    }

    @PostMapping
    public R<Employee> add(@RequestBody Employee employee, HttpServletRequest req) {
        //HttpSession session = req.getSession();
        //Long id = (Long) session.getAttribute("one");
        //employee.setCreateUser(id);
        //employee.setUpdateUser(id);
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);

        return R.success(employee);
    }

    @PutMapping
    public R update(@RequestBody Employee employee, HttpServletRequest req) {
        //从seesion中获取当前登陆人得id信息
        //long id = (long) req.getSession().getAttribute("one");
        //
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(id);
        //employee.setCreateUser(id);
        employeeService.updateById(employee);

        return R.success("修改成功");

    }

    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable Long id) {
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("id不存在");
    }

}
